package org.yocastlong.mybatisplus.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.yocastlong.mybatisplus.service.RedisService;

import java.util.concurrent.TimeUnit;

/**
 * @author 朱海龙
 * 2025/6/10 21:47
 */
@Service
public class RedisServiceImpl implements RedisService {

    @Autowired
    private RedisTemplate<String, Object> objectRedisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void setStringValue(String key, String value) {
        stringRedisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void setStringValue(String key, String value, long timeout) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);

    }

    @Override
    public void setStringValue(String key, String value, long timeout, TimeUnit unit) {
        stringRedisTemplate.opsForValue().set(key, value, timeout, unit);
    }

    @Override
    public String getStringValue(String key) {
        return stringRedisTemplate.opsForValue().get(key);
    }

    @Override
    public <T> T getObjectValue(String key) {
        return ((T) objectRedisTemplate.opsForValue().get(key));
    }

    @Override
    public <T> void setObjectValue(String key, T value) {
        objectRedisTemplate.opsForValue().set(key, value);
    }

}
